<script setup>
import { ref, onMounted } from 'vue'
import easyapi from '../api'
import { ElMessage, ElMessageBox, pageHeaderEmits } from 'element-plus'
//定义绑定的数据
const tableData = ref([]);
//控制新增信息窗口是否显示
const addWinVisible = ref(false);
//新增数据绑定对象
const addObject = ref({});
//控制新增信息窗口是否显示
const editWinVisible = ref(false);
//编辑数据绑定对象
const editObject = ref({});
//记录编辑数据的下标
var editIndex = -1;
//分页的总页数
const total = ref(100)
//查询数据输入框的绑定对象
const checkObj = ref({
    size: 3,
    page: 1
})

//教练列表
const types = ref([])

const cascaderProps = {
    // 配置 emitPath 为 false，使得选中的值不是数组而是具体值  
    emitPath: false,
    value: 'value',
    label: 'label',
    children: 'children'
}



onMounted(async function () {
    let result = await easyapi.get('/api/alltrainer')
    if (result.data) {
        for (let i = 0; i < result.data.length; i++) {
            // 使用 push 方法向 types 数组添加新对象  
            types.value.push({
                label: result.data[i].name,
                value: result.data[i].trainerID
            });
        }
    }
    checkData();
})

//删除数据
const del = function (index, row) {
    ElMessageBox.confirm(
        '确认删除该行数据吗?',
        '警告',
        {
            confirmButtonText: '确认',
            cancelButtonText: '取消',
            type: 'warning',
        }
    )
        .then(async () => {
            //提交数据
            await easyapi.del('/api/course', row.id)
            //刷新表单数据
            tableData.value.splice(index, 1)
            if (tableData.value.length == 0) {
                checkObj.value.page = (checkObj.value.page - 1) < 1 ? 1 : checkObj.value.page - 1;
                checkData(checkObj.value.page)
            } else {
                checkData();
            }
            //提示删除成功
            ElMessage({
                type: 'success',
                message: '删除成功',
            })
        })
        .catch(() => {
            ElMessage({
                type: 'info',
                message: '取消删除',
            })
        })
}

//新增数据
//显示新增窗口
const openAddWin = async function () {
    addObject.value = {}
    addWinVisible.value = true;
}
//关闭新增窗口
const closeAddWin = function () {
    addWinVisible.value = false;
}
//上次新增数据
const addSave = async function () {
    addForm.value.validate(async (valid) => {
        if (valid) {
            //保存新增的数据 addObject
            //发送请求
            let result = await easyapi.postJson('/api/public', addObject.value);
            //当前页行数小于size则将数据添加到该页
            if (tableData.value.length < checkObj.value.size) {
                tableData.value.push(result.data);
            } else {
                checkData();
            }
            //刷新表单
            //提示新增成功
            ElMessage({
                type: 'success',
                message: '新增数据成功',
            })
            //关闭窗口
            closeAddWin()
        } else {
            ElMessage({
                type: 'info',
                message: '格式错误',
            })
        }
    })
}

//修改数据
//显示编辑窗口
const openEditWin = function () {
    editWinVisible.value = true;
}
//关闭编辑窗口
const closeEditWin = function () {
    editWinVisible.value = false;
}
//点击编辑按钮，执行的方法
const edit = function (index, row) {
    //绑定要修改的数据
    editObject.value.id = row.id;
    editObject.value.name = row.name;
    editObject.value.trainerID = row.trainerID;
    editObject.value.introduction = row.introduction;
    editObject.value.date = row.date;
    editObject.value.address = row.address;
    editIndex = index;
    //打开对话框
    openEditWin();
}
//保存修改编辑数据
const editSave = async () => {
    editForm.value.validate(async (valid) => {
        if (valid) {
            //发送请求
            let result = await easyapi.putJson('/api/public', editObject.value)
            //刷新表单
            tableData.value[editIndex] = result.data;
            //提示
            ElMessage({
                type: 'success',
                message: '编辑数据成功',
            })
            //关闭窗口
            closeEditWin()
        } else {
            ElMessage({
                type: 'info',
                message: '格式错误',
            })
        }
    })
}

//查询功能
const checkData = async (val) => {
    //获取当前页数
    if (typeof val == 'number') {
        checkObj.value.page = val;
    }
    //查询数据
    let result = await easyapi.getSearch('/api/public', checkObj.value)
    //更新表格中的数据
    tableData.value = result.data.list;
    total.value = result.data.count;
}

//表单验证
const addForm = ref({})
const editForm = ref({})

const addCheckRules = {
    name: [
        { required: true, message: '请输入姓名', trigger: 'blur' },
    ],
    trainerID: [
        { required: true, message: '请选择课程教练', trigger: 'blur' },
    ],
    introduction: [
        { required: true, message: '请输入课程介绍', trigger: 'blur' },
    ],
    date: [
        { required: true, message: '请填入上课时间', trigger: 'blur' },
    ],
    address: [
        { required: true, message: '请输入上课地点', trigger: 'blur' },
    ]
}

const editCheckRules = {
    name: [
        { required: true, message: '请输入姓名', trigger: 'blur' },
    ],
    trainerID: [
        { required: true, message: '请选择课程教练', trigger: 'blur' },
    ],
    introduction: [
        { required: true, message: '请输入课程介绍', trigger: 'blur' },
    ],
    date: [
        { required: true, message: '请填入上课时间', trigger: 'blur' },
    ],
    address: [
        { required: true, message: '请输入上课地点', trigger: 'blur' },
    ]
}

//预约的学员
//预约公共课
const bookWinVisible = ref(false)
const bookObject = ref({})
const bookForm = ref({})
const bookData = ref([])

const openBookWin = function () {
    bookWinVisible.value = true;
}

const closeBookWin = function () {
    bookWinVisible.value = false;
}

const book = async function (index, row) {
    bookData.value = []
    //查询数据
    let result = await easyapi.getByID('/api/memberbook', row.id)
    //更新表格中的数据
    bookData.value = result.data;
    //打开对话框
    openBookWin();
}

const cancel = function (index, row) {
    ElMessageBox.confirm(
        '确认取消预定吗?',
        '提示',
        {
            confirmButtonText: '确认',
            cancelButtonText: '取消',
            type: 'warning',
        }
    )
        .then(async () => {
            //提交数据
            await easyapi.del('/api/book', row.id)
            //刷新表单数据
            bookData.value.splice(index, 1)
            //提示删除成功
            ElMessage({
                type: 'success',
                message: '取消成功',
            })
        })
}
</script>
<template>
    <el-input v-model="checkObj.name" style="width: 150px" placeholder="请输入查询的课程名称" />
    <el-input v-model="checkObj.trainerName" style="width: 150px" placeholder="请输入查询的教练名" />
    <el-button type="primary" @click="checkData">查询</el-button>
    <el-button type="primary" @click="openAddWin">新增</el-button>
    <el-table :data="tableData" :header-cell-style="{ 'text-align': 'center' }" :cell-style="{ 'text-align': 'center' }"
        style="width: 100%">
        <el-table-column prop="name" label="活动名" />
        <el-table-column prop="trainerName" label="活动教练" />
        <el-table-column prop="introduction" label="活动介绍" />
        <el-table-column prop="date" label="时间" />
        <el-table-column prop="address" label="活动地点" />
        <el-table-column label="操作" width="200px" header-align="center">
            <template #default="scope">
                <el-button type="primary" size="small" @click="book(scope.$index, scope.row)">学员</el-button>
                <el-button type="danger" size="small" @click="del(scope.$index, scope.row)">删除</el-button>
                <el-button type="success" size="small" @click="edit(scope.$index, scope.row)">编辑</el-button>
            </template>
        </el-table-column>
    </el-table>
    <!-- 分页组件初始显示的页码 -->
    <!-- size:每页的条数 -->
    <el-pagination :page-size="checkObj.size" :current-page="checkObj.page" background layout="prev, pager, next"
        :total="total" @current-change="checkData" />


    <!--start 新增信息的弹出框 -->
    <el-dialog v-model="addWinVisible" title="新增公共课数据" width="500">
        <el-form :model="addObject" :rules="addCheckRules" ref="addForm">
            <el-form-item label="课程名称" :label-width="80" prop="name">
                <el-input v-model="addObject.name" autocomplete="off" />
            </el-form-item>
            <el-form-item label="课程教练" :label-width="80" prop="trainerID">
                <el-cascader :props="cascaderProps" v-model="addObject.trainerID" :options="types" />
            </el-form-item>
            <el-form-item label="课程介绍" :label-width="80" prop="introduction">
                <el-input v-model="addObject.introduction" autocomplete="off" />
            </el-form-item>
            <el-form-item label="课程时间" :label-width="80" prop="date">
                <el-date-picker value-format="YYYY-MM-DD HH:mm:ss" v-model="addObject.date" type="datetime" placeholder="请选择活动开始时间" />
            </el-form-item>
            <el-form-item label="地点" :label-width="80" prop="address">
                <el-input v-model="addObject.address" autocomplete="off" />
            </el-form-item>
        </el-form>
        <template #footer>
            <div class="dialog-footer">
                <el-button @click="closeAddWin">取消</el-button>
                <el-button type="primary" @click="addSave">
                    确认
                </el-button>
            </div>
        </template>
    </el-dialog>
    <!--end 新增信息的弹出框 -->

    <!--start 修改信息的弹出框 -->
    <el-dialog v-model="editWinVisible" title="修改公共课数据" width="500">
        <el-form :model="editObject" :rules="editCheckRules" ref="editForm">
            <el-form-item label="课程名称" :label-width="80" prop="name">
                <el-input v-model="editObject.name" autocomplete="off" />
            </el-form-item>
            <el-form-item label="课程教练" :label-width="80" prop="trainerID">
                <el-cascader :props="cascaderProps" v-model="editObject.trainerID" :options="types" />
            </el-form-item>
            <el-form-item label="课程介绍" :label-width="80" prop="introduction">
                <el-input v-model="editObject.introduction" autocomplete="off" />
            </el-form-item>
            <el-form-item label="课程时间" :label-width="80" prop="date">
                <el-date-picker value-format="YYYY-MM-DD HH:mm:ss" v-model="editObject.date" type="datetime" placeholder="请选择活动开始时间" />
            </el-form-item>
            <el-form-item label="地点" :label-width="80" prop="address">
                <el-input v-model="editObject.address" autocomplete="off" />
            </el-form-item>
        </el-form>
        <template #footer>
            <div class="dialog-footer">
                <el-button @click="closeEditWin">取消</el-button>
                <el-button type="primary" @click="editSave">
                    确认
                </el-button>
            </div>
        </template>
    </el-dialog>
    <!--end 修改信息的弹出框 -->


    <!--start 查看预约学员的弹出框 -->
    <el-dialog v-model="bookWinVisible" title="会员预约" width="1000">
        <el-table :data="bookData" :header-cell-style="{ 'text-align': 'center' }" :cell-style="{ 'text-align': 'center' }"
        style="width: 100%">
        <el-table-column prop="memberName" label="会员姓名"/>
        <el-table-column prop="memberPhoneNumber" label="会员电话号码" />
        <el-table-column label="操作" header-align="center">
            <template #default="scope">
                <el-button type="danger" size="small" @click="cancel(scope.$index, scope.row)">取消预定</el-button>
            </template>
        </el-table-column>
    </el-table>
        <template #footer>
            <div class="dialog-footer">
                <el-button type="primary" @click="closeBookWin">
                    确认
                </el-button>
            </div>
        </template>
    </el-dialog>
    <!--end 查看预约学员的弹出框 -->

</template>
<style></style>